<template>
  <div class="wrap">
    <div class="tab">
      <p :class="tabIndex==1?'active':''" @click="setTabIndex(1)">我是客户</p>
      <p :class="tabIndex==2?'active':''" @click="setTabIndex(2)">我是供应商</p>
    </div>
    <div class="search">
      <input type="text" v-model="username" @keyup.enter="query" placeholder="请输入名称搜索" />
      <img src="../assets/search.png" @click="query" alt />
    </div>
    <div class="dateBox">
      <div class="lkbox">
        <div class="lk" @click="orderState = true" v-if="tabIndex==1">
          <p :class="sendStateTag==1?'act':''">{{sendStateName}}</p>
          <img :src="sendStateTag==1?require('../assets/top2.png'):require('../assets/zx.png')" alt />
        </div>
      </div>
      <div class="date" @click="dateState = true">
        <p>{{date?date:"日期筛选"}}</p>
        <img :src="date?require('../assets/top.png'):require('../assets/bot.png')" alt />
      </div>
    </div>
    <template v-if="tabIndex==1">
      <div class="list">
        <!-- <van-index-bar> -->
        <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
          <div class="item" v-for="item in q_list" :key="item.user_id" @click="next(item)">
            <div class="left">
              <img :src="item.avatar" :onerror="defaultImg" alt />
              <div>
                <p>{{item.username}}</p>
                <span>订单数：{{item.total}}</span>
              </div>
            </div>
            <img src="../assets/right.png" class="right" alt />
          </div>
          <van-empty description="暂无数据" v-if="state1==1" />
        </van-pull-refresh>
        <!-- </van-index-bar> -->
      </div>
    </template>
    <template v-if="tabIndex==2">
      <div class="list">
        <!-- <van-index-bar> -->
        <van-pull-refresh v-model="refreshing2" @refresh="onRefresh2">
          <div class="item" v-for="(item,idx) in s_list" :key="idx" @click="details(item,idx)">
            <div class="left">
              <img :src="item.avatar" :onerror="defaultImg" alt />
              <div>
                <p>{{item.username}}</p>
                <span>订单数：{{item.total}}</span>
              </div>
            </div>
            <img src="../assets/right.png" class="right" alt />
          </div>
          <van-empty description="暂无数据" v-if="state2==1" />
        </van-pull-refresh>
        <!-- </van-index-bar> -->
      </div>
    </template>
    <van-popup v-model="dateState" position="bottom">
      <van-datetime-picker
        v-model="createDate"
        type="date"
        @confirm="dateConfirm"
        @cancel="dateCancel"
        cancel-button-text="全部"
      />
    </van-popup>
    <van-action-sheet
      v-model="orderState"
      description="选择订单状态"
      :round="false"
      :actions="sendState"
      @select="onOrder"
    />
  </div>
</template>
<style>
.van-pull-refresh__track {
  height: 60vh;
}
</style>
<style lang="scss" scoped>
.tab {
  display: flex;
  width: 9.2rem;
  margin: 10px auto 0;
  justify-content: space-around;
  p {
    font-size: 0.4rem;
    color: #555;
    line-height: 1.066667rem;
    width: 40%;
  }
  .active {
    color: #3fd9c9;
    border-bottom: 2px solid #3fd9c9;
  }
}
.search {
  background: #eeeded;
  height: 0.9rem;
  width: 9.2rem;
  margin: 0.306667rem auto 0.4rem;
  border-radius: 0.266667rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
  input {
    border: none;
    outline: none;
    background: transparent;
    margin-left: 15px;
  }
  img {
    width: 0.533333rem;
    height: 0.533333rem;
    display: block;
    margin-right: 15px;
  }
}
.dateBox {
  display: flex;
  justify-content: space-between;
  margin-bottom: 0.4rem;
  .date {
    padding: 0 0.5rem;
    height: 0.613333rem;
    background: #3fd9c9;
    border-radius: 0.306667rem;
    margin-right: 0.4rem;
    display: flex;
    justify-content: space-around;
    color: #fff;
    font-size: 0.306667rem;
    align-items: center;
    p:nth-of-type(2) {
      margin-left: 0.5rem;
    }
    img {
      width: 10px;
      margin-left: 4px;
    }
  }
  .lkbox {
    display: flex;
  }
  .lk {
    display: flex;
    align-items: center;
    margin-left: 0.4rem;
    img {
      width: 10px;
      margin-left: 4px;
      margin-top: 2px;
    }
    p {
      font-size: 0.32rem;
      color: #999;
    }
    .act {
      color: #3fd9c9;
    }
  }
}
.item {
  height: 1.6rem;
  border-bottom: 1px solid #e5e5e5;
  width: 9.2rem;
  margin: 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  .left {
    display: flex;
    align-items: center;
    img {
      width: 1.013333rem;
      height: 1.013333rem;
      display: block;
      border-radius: 50%;
      margin-right: 0.186667rem;
      object-fit: cover;
    }
    p {
      color: #555;
      font-size: 0.4rem;
      text-align: left;
    }
    span {
      color: #999;
      font-size: 0.25rem;
      text-align: left;
      display: block;
    }
  }
  .right {
    display: block;
    width: 0.4rem;
    height: 0.4rem;
  }
}
>>> .van-index-bar__index {
  font-size: 12px;
  line-height: 20px;
}
</style>
<script>
import {
  getTakeList,
  getDeliveryList,
  getById,
  editUser
} from "@/utils/interface";
import { formatDate } from "@/utils/tools";
import { Empty } from "vant";

export default {
  components: {
    Empty
  },
  data() {
    return {
      tabIndex: 1,
      defaultImg: 'this.src="' + require("../assets/user2.png") + '"',
      isLoading: false,
      q_list: [],
      s_list: [],
      username: "",
      createDate: new Date(),
      dateState: false,
      date: "",
      loading: false,
      finished: true,
      refreshing: false,
      loading2: false,
      finished2: true,
      refreshing2: false,
      state1: 0,
      state2: 0,
      sendStateName: "所有状态",
      sendStateTag: 0,
      orderState: false,
      orderState: false,
      sendState: [
        { name: "全部", value: "" },
        { name: "已到货", value: 1 },
        { name: "未到货", value: 0 }
      ],
      orderValue:""
    };
  },
  created() {
    let date = sessionStorage.getItem("index-date");
    let orderValue = sessionStorage.getItem('orderValue')
    let sendStateName = sessionStorage.getItem('sendStateName')
    date ? (this.date = date) : "";
    orderValue?(this.orderValue=orderValue,this.sendStateTag = 1,this.sendStateName=sendStateName):''
    console.log(orderValue)
    document.title = "全部订单";
    this.getTakeList(this.username, this.date,this.orderValue);
    this.getDeliveryList(this.username, this.date,this.orderValue);
    this.getUser();
  },
  methods: {
    onOrder(item) {
      console.log(item)
      if(item.value==1||item.value=='0'){
        this.sendStateTag = 1
      }else if(item.value==""){
        this.sendStateTag = 0
      }
      console.log(this.sendStateTag)
      this.sendStateName = item.name == "全部" ? "所有状态" : item.name;
      // 供应商搜索
      this.orderValue = item.value;
      sessionStorage.setItem('orderValue', this.orderValue)
      sessionStorage.setItem('sendStateName', this.sendStateName )
      
      if (this.tabIndex == 1) {
          this.getTakeList(this.username, this.date,this.orderValue);
      }
      if(this.tabIndex == 2) {
        this.getDeliveryList(this.username, this.date,this.orderValue);
      }
      
      this.orderState = false;
    },
    setTabIndex(index) {
      if (index == 1) {
        // 客户搜索
        this.getTakeList(this.username, this.date,this.orderValue);
      }
      if (index == 2) {
        this.getDeliveryList(this.username, this.date,this.orderValue);
      }
      this.tabIndex = index;
    },
    next(item) {
      this.$router.push({
        path: "/order",
        query: { shopId: item.user_id, info: JSON.stringify(item) ,state:this.orderValue,date:this.date}
      });
    },
    // 取货单商家列表
    getTakeList(username, createDate,send_state) {
      let sendState2 = "";
      sendState2 = send_state>=0?String(send_state):"";
      getTakeList({ username, createDate ,send_state:sendState2}).then(res => {
        this.q_list = res.data.data;
        if (this.q_list.length == 0) {
          this.state1 = 1;
        }
        this.loading = false;
        this.refreshing = false;
        this.finished = true;
      });
    },
    // 送货单列表
    getDeliveryList(username, createDate,send_state) {
      let sendState2 = "";
      sendState2 = String(send_state);
      getDeliveryList({ username, createDate,send_state }).then(res => {
        this.s_list = res.data.data;
        if (this.s_list.length == 0) {
          this.state2 = 1;
        }
        this.loading2 = false;
        this.refreshing2 = false;
        this.finished2 = true;
      });
    },
    dateConfirm(e) {
      console.log(e);
      this.dateState = false;
      this.date = formatDate(e);
      sessionStorage.setItem("index-date", this.date);
      if (this.tabIndex == 1) {
        this.getTakeList(this.username, this.date,this.orderValue);
      } else if (this.tabIndex == 2) {
         this.getDeliveryList(this.username, this.date,this.orderValue);
      }
    },
    dateCancel() {
      this.date = "";
      this.dateState = false;
      sessionStorage.setItem("index-date", "");
      if (this.tabIndex == 1) {
         this.getTakeList(this.username, this.date,this.orderValue);
      } else if (this.tabIndex == 2) {
         this.getDeliveryList(this.username, this.date,this.orderValue);
      }
    },
    query() {
      if (this.tabIndex == 1) {
       this.getTakeList(this.username, this.date,this.orderValue);
      } else if (this.tabIndex == 2) {
        this.getDeliveryList(this.username, this.date,this.orderValue);
      }
    },
    details(item, n) {
      this.$router.push({
        path: "/decvtio",
        query: { shopId: item.user_id, info: JSON.stringify(item), id: n }
      });
    },
    getUser() {
      getById().then(res => {
        if (res.data.code == 200) {
          if (!res.data.data.avatar) {
            let mobileInfo = JSON.parse(localStorage.getItem("mobileInfo"));
            editUser({
              avatar: mobileInfo.headimgurl,
              username: mobileInfo.nickname
            }).thern(res => {
              console.log(res.data);
            });
          }
        } else {
          localStorage.removeItem("customerwx");
          localStorage.removeItem("wechataccess_user_token");
          localStorage.removeItem("mobileInfo");
          this.$router.replace("/login");
        }
      });
    },
    onRefresh() {
      // 清空列表数据
      this.finished = false;
      this.loading = true;
       this.getTakeList(this.username, this.date,this.orderValue);
    },
    onRefresh2() {
      // 清空列表数据
      this.finished2 = false;
      this.loading2 = true;
       this.getDeliveryList(this.username, this.date,this.orderValue);
    }
  }
};
</script>